package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-21
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    @Override
    public ResponseResult listByName(SensitiveDto dto) {

        IPage<WmSensitive> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> query=new LambdaQueryWrapper<>();
        if(dto.getName()!=null||dto.getName()!=""){
            query.like(WmSensitive::getSensitives,dto.getName());
        }
        IPage<WmSensitive> iPage = this.page(page,query);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
        return result;
    }

    @Override
    public ResponseResult saveSensitive(SensitiveDto dto) {
        if(dto.getSensitives()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive sensitive=new WmSensitive();
        sensitive.setSensitives(dto.getSensitives());
        sensitive.setCreatedTime(new Date());
        this.save(sensitive);
        return ResponseResult.okResult();
    }
}
